package pl.pr3sto.security

import grails.test.*

class PersonTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }
	
	void testToString() {
		mockDomain(Person)
		
		// given
		Person person = new Person(email: 'email@afas.pl', firstName: 'Zuzanna', lastName: 'Kuna', city: 'aaa', address: 'aaa', phone: '123', postCode: '123')
		
		// when/then
		assert person.toString() == 'Zuzanna Kuna'
	}
		
    void testConstraintEmailUnique() {
		mockForConstraintsTests(Person)
		mockDomain(Person)
		
		// given
		new Person(email: 'email@afas.pl', firstName: 'aaa', lastName: 'aaa', city: 'aaa', address: 'aaa', phone: '123', postCode: '123').save()
		Person newPerson = new Person(email: 'email@afas.pl', firstName: 'aaa', lastName: 'aaa', city: 'aaa', address: 'aaa', phone: '123', postCode: '123')
		
		// when
		newPerson.validate()
		
		// then
		assert newPerson.errors.getFieldError('email').code == 'unique'
    }

    void testConstraintEmailEmail() {
		mockForConstraintsTests(Person)
		mockDomain(Person)
		
		// given
		Person newPerson = new Person(email: '.pl', firstName: 'aaa', lastName: 'aaa', city: 'aaa', address: 'aaa', phone: '123', postCode: '123')
		
		// when
		newPerson.validate()
		
		// then
		assert newPerson.errors.getFieldError('email').code == 'email.invalid'
    }
}
